package cn.dapeng.base;

import cn.dapeng.util.SleepingUtil;

import java.util.concurrent.TimeUnit;


/**
 * 使用线程自带的interrupt方法，实现优雅关闭, 不能做到精准关闭，比如循环5次就关闭
 */
public class ThreadStop_T04 {

    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            while (!Thread.interrupted()) {

            }
            System.out.println("thread end");
        });

        thread.start();

        SleepingUtil.sleep(5, TimeUnit.SECONDS);
        thread.interrupt();
    }
}
